package com.hc.common.utils;

import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import java.util.Properties;

public class EmailUtil {

    public static void send(String recipient, String subject, String content) {
        Properties props = new Properties();
        props.setProperty("mail.transport.protocol", "smtp");//开启协议
        props.setProperty("mail.smtp.host", "smtp.qq.com");//主机名
        props.setProperty("mail.smtp.auth", "true");//是否开启权限控制
        props.setProperty("mail.debug", "true");//true表示在发送邮件时会在控制台上打印发送时的信息

        //Session表示程序到邮件服务器之间的一次会话
        Session session = Session.getInstance(props);
        Message msg = new MimeMessage(session);//邮件对象
        try {
            msg.setFrom(new InternetAddress("1819902916@qq.com"));//发送人
            msg.setRecipient(Message.RecipientType.TO, new InternetAddress(recipient));//收件人
            msg.setSubject(subject);//主题
            msg.setText(content);//正文

            Transport tran = session.getTransport();
            tran.connect("1819902916", "qrznqxbrvqvxbdfh");//此处不是QQ的密码，是开启POP3/SMTP时生成的authorization code
            tran.sendMessage(msg, msg.getAllRecipients());
        } catch (MessagingException e) {
            e.printStackTrace();
        }
    }

//    public static void main(String[] args) {
//        String subject = "来自微风商城的激活邮箱";
//        String content = "点击如下连接激活账户，如果不能点击请复制到浏览器地址栏访问：http://www.wego.com/userServlet?op=active&code=432141";
//        EmailUtil.send("1502649204@qq.com", subject, content);
//    }
}
